package hik.jpeg;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * 抓图带温度数据
 * 
 * @author fankai
 *
 */
public class CatchJpegWithData {
	
	public static void main(String[] args) {
		DataInputStream dis = null ;
		try {
//			dis = new DataInputStream(new FileInputStream("C:\\Users\\fankai\\Desktop\\图谱分析\\160_120.txt"));
			dis = new DataInputStream(new FileInputStream("C:\\Users\\fankai\\Desktop\\图谱分析\\1589287066-2.fir"));
//			System.out.println(dis.read());
			byte[] fir = new byte[4];
			dis.read(fir, 0, 4);
			
//			System.out.println(dis.read());
			System.out.println(new String(fir));
			dis.read(fir, 0, 4);
			System.out.println(getFloat(fir));
			dis.read(fir, 0, 4);
			System.out.println(getFloat(fir));
			dis.read(fir, 0, 4);
			System.out.println(getFloat(fir));
			dis.read(fir, 0, 4);
			System.out.println(getFloat(fir));
			dis.read(fir, 0, 4);
			System.out.println(getFloat(fir));
			dis.read(fir, 0, 2);
			System.out.println(byteToShort(fir));
			dis.read(fir, 0, 2);
			System.out.println(byteToShort(fir));
			
			fir = new byte[34];
			dis.read(fir, 0, 34);
			fir = new byte[2];
			dis.read(fir, 0, 2);
			while (fir.length > 0) {
				System.out.println(byteToShort(fir));
				dis.read(fir, 0, 2);
			}
			
//			short readShort = dis.readShort();
//			System.out.println(readShort);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			if (dis != null) {
				try {
					dis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	
	public static void writeFile(String filePath,short height,short width,short[] data) {
		byte[] head = new byte[64];
		String suffix = "FIR";
		byte[] suffixBs = suffix.getBytes();
		addBytes(head, 0, suffixBs);

		// 光学透过率
		Float light = 0.96F;
		addFloatBytes(head, 4, light);

		// 比辐射率
		Float emiss = 0.96F;
		addFloatBytes(head, 8, emiss);

		// 距离
		Float distance = 20.00F;
		addFloatBytes(head, 12, distance);

		// 环境温度
		Float temp = 30.00F;
		addFloatBytes(head, 16, temp);

		// 环境湿度
		Float humidity = 80.00F;
		addFloatBytes(head, 20, humidity);

		// 高度
		byte[] height2byte = short2Byte(height);
		addBytes(head, 24, height2byte);

		// 宽度
		byte[] width2byte1 = short2Byte(width);
		addBytes(head, 26, width2byte1);
		
		try {
			File file = new File(filePath);
			if (!file.getParentFile().exists()) {
				file.mkdirs();
			}
			
			//文件输出流
			FileOutputStream outputStream = new FileOutputStream(file);
			//数据输出流
			DataOutputStream dataOutputStream = new DataOutputStream(outputStream);
			//给person.data这个文件写入数据
			System.out.println("数据长度："+data.length);
			dataOutputStream.write(head);
			for (short i : data) {
				dataOutputStream.write(short2Byte(i));
			}
			//关闭流
			dataOutputStream.flush();
			dataOutputStream.close();
			outputStream.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static float getFloat(byte[] b) {
		int accum = 0;
		accum = accum | (b[0] & 0xff) << 0;
		accum = accum | (b[1] & 0xff) << 8;
		accum = accum | (b[2] & 0xff) << 16;
		accum = accum | (b[3] & 0xff) << 24;
		return Float.intBitsToFloat(accum);
	}
	
	/**
	 * byte数组写入文件
	 * 
	 * @param filePath
	 * @param data
	 * @throws Exception
	 */
	public static void saveByteFile(String filePath, byte[] data) throws Exception {
		if (data != null) {
			File file = new File(filePath);
			if (!file.getParentFile().exists()) {
				file.mkdirs();
			}
			FileOutputStream fos = new FileOutputStream(file);
			fos.write(data, 0, data.length);
			fos.flush();
			fos.close();
		}
	}

	public static void addBytes(byte[] head, int i, byte[] data) {
		for (byte b : data) {
			head[i] = b;
			i++;
		}
	}

	/**
	 * 向byte 数组中添加 byte数据
	 * 
	 * @param head byte数组
	 * @param i    起始位
	 * @param data 添加的数据
	 */
	public static void addFloatBytes(byte[] head, int i, float data) {
		byte[] bytes = float2byte(data);
		for (byte b : bytes) {
			head[i] = b;
			i++;
		}
	}
	
	  /** 
     * 注释：字节数组到short的转换！ 
     * 
     * @param b 
     * @return 
     */ 
    public static short byteToShort(byte[] b) { 
        short s = 0; 
        short s0 = (short) (b[0] & 0xff);// 最低位 
        short s1 = (short) (b[1] & 0xff); 
        s1 <<= 8; 
        s = (short) (s0 | s1); 
        return s; 
    }
	
	public static byte[] short2Byte(short x){
		 byte high = (byte) (0x00FF & (x>>8)); //定义第一个byte        
		 byte low = (byte) (0x00FF & x); //定义第二个byte
		 byte[] bytes = new byte[2];
		 bytes[0] = low;
		 bytes[1] = high;
		 return bytes;
	 }

	// 这个函数将float转换成byte[]
	public static byte[] float2byte(float f) {

		// 把float转换为byte[]
		int fbit = Float.floatToIntBits(f);

		byte[] b = new byte[4];
		for (int i = 0; i < 4; i++) {
			b[i] = (byte) (fbit >> (24 - i * 8));
		}

		// 翻转数组
		int len = b.length;
		// 建立一个与源数组元素类型相同的数组
		byte[] dest = new byte[len];
		// 为了防止修改源数组，将源数组拷贝一份副本
		System.arraycopy(b, 0, dest, 0, len);
		byte temp;
		// 将顺位第i个与倒数第i个交换
		for (int i = 0; i < len / 2; ++i) {
			temp = dest[i];
			dest[i] = dest[len - i - 1];
			dest[len - i - 1] = temp;
		}
		return dest;
	}

	/**
	 * 16进制字符串转bytes
	 * 
	 * @param hex
	 * @return
	 */
	public static byte[] hexStringToByte(String hex) {
		int len = 0;
		int num = 0;
		// 判断字符串的长度是否是两位
		if (hex.length() >= 2) {
			// 判断字符喜欢是否是偶数
			len = (hex.length() / 2);
			num = (hex.length() % 2);
			if (num == 1) {
				hex = "0" + hex;
				len = len + 1;
			}
		} else {
			hex = "0" + hex;
			len = 1;
		}
		byte[] result = new byte[len];
		char[] achar = hex.toCharArray();
		for (int i = 0; i < len; i++) {
			int pos = i * 2;
			result[i] = (byte) (toByte(achar[pos]) << 4 | toByte(achar[pos + 1]));
		}
		return result;
	}

	private static int toByte(char c) {
		if (c >= 'a')
			return (c - 'a' + 10) & 0x0f;
		if (c >= 'A')
			return (c - 'A' + 10) & 0x0f;
		return (c - '0') & 0x0f;
	}

	public static byte[] int2byte(int s) {
		byte[] b = new byte[4];
		for (int i = 0; i < 4; i++) {
			int offset = 16 - (i + 1) * 8; // 因为byte占4个字节，所以要计算偏移量
			b[i] = (byte) ((s >> offset) & 0xff); // 把32位分为4个8位进行分别存储
		}
		return b;
	}

	private static void write(String filePath,short[] data) throws IOException {
		// DataOutputStream(OutputStream out)
		// 创建数据输出流对象
		File file = new File(filePath);
		if (!file.getParentFile().exists()) {
			file.mkdirs();
		}
		DataOutputStream dos = new DataOutputStream(new FileOutputStream(file));
		for (short i : data) {
			dos.writeShort(i);
		}
		// 写数据了
		// 释放资源
		dos.close();
	}

}
